package com.goal98.grails.plugin.account.domain

class Account {

    String reference
    BigDecimal balance

    BigDecimal getAmount(){
        BigDecimal amount = 0
        transactions.each{
            boolean isCredit = it instanceof Credit
            amount += isCredit? it?.amount:it?.amount*-1
        }
        return amount
    }

    List transactions
    static hasMany = [transactions:Transaction]
    static transients = ['amount']

    static constraints = {
    }

    String toString(){
        return reference
    }

    void debit(BigDecimal amount){
        balance -= amount

    }

    void credit(BigDecimal amount){
        balance += amount
    }

    static mapping = {
        cache true
    }
}
